/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.ordersstore.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import com.github.wxpay.sdk.WXPayUtil;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.*;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.address.entity.Address;
import com.jeeplus.modules.address.mapper.AddressMapper;
import com.jeeplus.modules.centerposition.entity.CenterPosition;
import com.jeeplus.modules.centerposition.service.CenterPositionService;
import com.jeeplus.modules.centerrecord.entity.CenterRecord;
import com.jeeplus.modules.centerrecord.service.CenterRecordService;
import com.jeeplus.modules.dynamicrecord.entity.DynamicRecord;
import com.jeeplus.modules.dynamicrecord.service.DynamicRecordService;
import com.jeeplus.modules.express.entity.Express;
import com.jeeplus.modules.express.service.ExpressService;
import com.jeeplus.modules.goods.entity.Goods;
import com.jeeplus.modules.goodsspecs.entity.GoodsSpec;
import com.jeeplus.modules.goodsspecs.service.GoodsSpecService;
import com.jeeplus.modules.goodsstore.entity.GoodsStore;
import com.jeeplus.modules.goodsstore.service.GoodsStoreService;
import com.jeeplus.modules.incomerecord.entity.IncomeRecord;
import com.jeeplus.modules.incomerecord.service.IncomeRecordService;
import com.jeeplus.modules.integraldetail.entity.IntegralDetail;
import com.jeeplus.modules.integraldetail.service.IntegralDetailService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.ordersstore.entity.OrdersStore;
import com.jeeplus.modules.ordersstore.mapper.OrdersStoreMapper;
import com.jeeplus.modules.replenishment.entity.Replenishment;
import com.jeeplus.modules.replenishment.service.ReplenishmentService;
import com.jeeplus.modules.rewardparameters.entity.RewardParameters;
import com.jeeplus.modules.rewardparameters.service.RewardParametersService;
import com.jeeplus.modules.teamrecord.entity.TeamRecord;
import com.jeeplus.modules.teamrecord.service.TeamRecordService;
import com.jeeplus.modules.wxgroup.entity.WxGroup;
import com.jeeplus.modules.wxgroup.service.WxGroupService;
import com.jeeplus.modules.wxpay.entity.WxPay;
import com.jeeplus.modules.wxpay.service.WxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 商城订单Service
 * @author lxj
 * @version 2020-11-06
 */
@Service
@Transactional(readOnly = true)
public class OrdersStoreService extends CrudService<OrdersStoreMapper, OrdersStore> {

	@Autowired
	private AddressMapper addressMapper;
	@Autowired
	private OrdersStoreService ordersStoreService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private GoodsStoreService goodsStoreService;
	@Autowired
	private GoodsSpecService goodsSpecService;
	@Autowired
	private ExpressService expressService;
	@Autowired
	private IntegralDetailService integralDetailService;
	@Autowired
	private CenterRecordService centerRecordService;
	@Autowired
	private TeamRecordService teamRecordService;
	@Autowired
	private RewardParametersService rewardParametersService;
	@Autowired
	private CenterPositionService centerPositionService;

	private final static String APPID = "wxf8333f0015bfad41";
	private final static String APPSECRET = "8104ad1318a445ace1bf2eb604330783";
	private final static String MCH_ID = "1607079032";
	private final static String PATERNERKEY = "yizhelegouyizhelegou111111686868";


	public OrdersStore get(String id) {
		return super.get(id);
	}
	public OrdersStore getByNo(String ordersNo){
		OrdersStore ordersStore = mapper.getByNo(ordersNo);
		if (ordersStore != null){
			GoodsSpec goodsSpec = goodsSpecService.get(ordersStore.getSpec());
			if (goodsSpec != null){
				ordersStore.setSpecName(goodsSpec.getSpecName());
				ordersStore.setGoodsPrice(goodsSpec.getSpecPrice());
				ordersStore.setSendIntegral(goodsSpec.getSpecIntegral());
			}else {
				GoodsStore goodsStore = goodsStoreService.get(ordersStore.getGoodsStore().getId());
				if (goodsStore != null){
					ordersStore.setSendIntegral(goodsStore.getIntegral());
				}
			}
		}
		return ordersStore;
	}
	
	public List<OrdersStore> findList(OrdersStore ordersStore) {
		List<OrdersStore> list = super.findList(ordersStore);
		return list;
	}
	
	public Page<OrdersStore> findPage(Page<OrdersStore> page, OrdersStore ordersStore) {
		dataRuleFilter(ordersStore);
		ordersStore.setPage(page);
		if (StringUtils.isNotBlank(ordersStore.getSpec())){
			Member memberByLoginName = memberService.getMemberByLoginName(ordersStore.getSpec());
			if (memberByLoginName != null){
				ordersStore.setSpec(memberByLoginName.getId());
			}else {
				page.setList(new ArrayList<>());
				return page;
			}

		}
		List<OrdersStore> list = super.findList(ordersStore);
		if (list.size()>0){
			for (OrdersStore store : list) {
				Member member = memberService.get(store.getSpec());
				if (member != null){
					store.setSpec(member.getLoginName());
				}
			}
		}
		page.setList(list);
		return page;
//		return super.findPage(page, ordersStore);
	}
	
	@Transactional(readOnly = false)
	public void save(OrdersStore ordersStore) {
		if (ordersStore.getServiceMem() != null){
			if (StringUtils.isNotBlank(ordersStore.getServiceMem().getId())){
				ordersStore.setSpec(ordersStore.getServiceMem().getId());
			}
		}
		super.save(ordersStore);
	}
	
	@Transactional(readOnly = false)
	public void delete(OrdersStore ordersStore) {
		super.delete(ordersStore);
	}


	/**
	 * 确认订单
	 * @param member  购买用户
	 * @param boughtGood   购买商品
	 * @return
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public AjaxJson conOrder(Member member, GoodsStore boughtGood, Integer num) {
		AjaxJson aj = new AjaxJson();
		//判断商品是否是上架中
		if (!"1".equals(boughtGood.getGoodsStatus())) {
			aj.setSuccess(false);
			aj.setMsg("该商品已下架");
			return aj;
		}
		if (boughtGood.getPrice() > 0){
			boughtGood.setShowPrice("1");
		}
		if (boughtGood.getIntegral() > 0){
			boughtGood.setShowPrice("0");
		}
//		Address address = addressMapper.get(addressId);
//		if (address == null){
//			aj.setSuccess(false);
//			aj.setMsg("地址信息有误");
//			return aj;
//		}

//		GoodsSpec goodsSpec = goodsSpecService.get(specId);
		OrdersStore ordersStore=new OrdersStore();
		ordersStore.setGoodsStore(boughtGood);
		ordersStore.setMember(member);
		String[] firstImg=boughtGood.getMainImgs().split("\\|");
//		if (goodsSpec != null){
//			firstImg=goodsSpec.getSpecPic().split("\\|");
//		}
		ordersStore.setGoodsPic(firstImg[0]);
		ordersStore.setGoodsPrice(boughtGood.getPrice());
		ordersStore.setGoodsNum(num);
//		ordersStore.setAddressName(address.getName());
//		ordersStore.setAddressMobile(address.getMobile());
//		ordersStore.setAddressAdd(address.getAddress());
//		ordersStore.setSpec(serviceId);
//		ordersStore.setSpec(specId);
//		ordersStore.setGoodsSpec(goodsSpec);
		aj.put("order",ordersStore);
		aj.setSuccess(true);
		return aj;
	}



	/**
	 * 提交订单
	 * @param member  购买用户
	 * @param boughtGood   购买商品
	 * @param addressId
	 *@param serviceId  @return
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public synchronized AjaxJson placeOrder(Member member, GoodsStore boughtGood, Integer num, String addressId, String serviceId) {
		AjaxJson aj = new AjaxJson();
		//积分中心不可兑换产品
		if ("1".equals(member.getIsIntegral())){
			aj.setSuccess(false);
			aj.setMsg("积分中心不可兑换产品");
			return aj;
		}
		//积分中心积分收益奖励比例
		Double percentIntegral = 0.0;
		//查询奖励比例
		List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
		if (rewardParametersList.size()>0){
			percentIntegral = rewardParametersList.get(0).getIntePercentIntegral();
		}
		//判断商品是否是上架中
		if (!"1".equals(boughtGood.getGoodsStatus())) {
			aj.setSuccess(false);
			aj.setMsg("该商品已下架");
			return aj;
		}
		Address address = addressMapper.get(addressId);
		if (address == null){
			aj.setSuccess(false);
			aj.setMsg("地址信息有误");
			return aj;
		}
		//判断库存是否充足
		Member mem = new Member();
		mem.setId(serviceId);
		CenterPosition centerPosition = new CenterPosition();
		centerPosition.setGoodsStore(boughtGood);
		centerPosition.setMember(mem);
		List<CenterPosition> centerPositions = centerPositionService.findList(centerPosition);
		if (centerPositions.size() > 0){
			if (centerPositions.get(0).getStock() < num){
				aj.setSuccess(false);
				aj.setMsg("商品库存不足");
				return aj;
			}else {
				centerPositions.get(0).setStock(centerPositions.get(0).getStock() - num);
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("商品库存不足");
			return aj;
		}
		//积分
		Double payIntegral = boughtGood.getIntegral()*num;
		//金额
		Double payMoney = boughtGood.getPrice()*num;

		//查询账户信息
		String accountId = member.getAccount().getId();
		Account account = accountService.get(accountId);
		if (account==null){
			aj.setMsg("账户信息错误");
			aj.setSuccess(false);
			return aj;
		}
		Double integral = account.getIntegral();
		Double balance = account.getYue();

		if (integral < payIntegral){
			aj.setMsg("积分不足");
			aj.setSuccess(false);
			return aj;
		}
		if (balance < payMoney){
			aj.setMsg("余额不足");
			aj.setSuccess(false);
			return aj;
		}
		//是否更新钱包
		boolean flag = false;
		//购买用户扣积分
		if (payMoney > 0){
			flag = true;
			account.setYue(balance - payMoney);
		}
		if (payIntegral > 0){
			flag = true;
			account.setIntegral(integral-payIntegral);
		}
		if (flag){
			accountService.save(account);
		}

		SnowFlake snowFlake = new SnowFlake(2, 3);
		//生成订单编号
		Long orderFormNumber = snowFlake.nextId();

		OrdersStore ordersStore=new OrdersStore();
		ordersStore.setAddressAdd(address.getAddress());
		ordersStore.setAddressMobile(address.getMobile());
		ordersStore.setAddressName(address.getName());
		ordersStore.setGoodsStore(boughtGood);
		ordersStore.setMember(member);
		ordersStore.setOrdersNo(orderFormNumber.toString());
		ordersStore.setOrdersStatus("2");
		ordersStore.setGoodsPic(boughtGood.getMainImgs());
		ordersStore.setGoodsPrice(boughtGood.getIntegral());
		ordersStore.setGoodsNum(num);
		String[] firstImg=boughtGood.getMainImgs().split("\\|");
		ordersStore.setGoodsPic(firstImg[0]);
		ordersStore.setSpec(serviceId);
		ordersStore.setIsExchange("0");
		ordersStore.setJifen(payIntegral);
		ordersStore.setMoney(payMoney);
		ordersStore.setPaytime(new Date());
		ordersStoreService.save(ordersStore);

		//增加该商品的购买人数
		boughtGood.setBoughtPerson(boughtGood.getBoughtPerson()+num);
		goodsStoreService.save(boughtGood);
		if (payIntegral > 0){
			//积分中心加积分
			Member integralMember = memberService.get(serviceId);
			Account integralAccount = accountService.get(integralMember.getAccount().getId());
			integralAccount.setIntegral(integralAccount.getIntegral()+payIntegral*(1-percentIntegral));
			integralAccount.setYue(integralAccount.getYue()+(payIntegral*percentIntegral));
			integralAccount.setTodayTeam(integralAccount.getTodayTeam()+(payIntegral*percentIntegral));
			integralAccount.setTeamProfit(integralAccount.getTeamProfit()+(payIntegral*percentIntegral));
			integralAccount.setTotalProfit(integralAccount.getTotalProfit()+(payIntegral*percentIntegral));
			integralAccount.setTodayProfit(integralAccount.getTodayProfit()+(payIntegral*percentIntegral));
			accountService.save(integralAccount);

			//商品兑换
			IntegralDetail integralDetail = new IntegralDetail();
			integralDetail.setMember(member);
			integralDetail.setIntegralType("1");
			integralDetail.setIntegral(payIntegral);
			integralDetail.setOrderNo(ordersStore.getOrdersNo());
			integralDetailService.save(integralDetail);
			//会员兑换
			IntegralDetail integralDetail1 = new IntegralDetail();
			integralDetail1.setMember(integralMember);
			integralDetail1.setIntegralType("2");
			integralDetail1.setIntegral(payIntegral*(1-percentIntegral));
			integralDetail1.setOrderNo(ordersStore.getOrdersNo());
			integralDetailService.save(integralDetail1);
			//记录积分中心 从用户积分购买 获利
			TeamRecord teamRecord = new TeamRecord();
			teamRecord.setMember(integralMember);
			teamRecord.setMoney(payIntegral*percentIntegral);
			teamRecord.setRemarks(ordersStore.getOrdersNo());
			teamRecordService.save(teamRecord);
			//处理服务中心积分兑换收益
			centerRecordService.manageCenterIntegral(member,payIntegral,ordersStore.getOrdersNo());
		}

		centerPositionService.save(centerPositions.get(0));

		aj.setMsg("支付成功");
		aj.setSuccess(true);
		//
		return aj;
	}





	/**
	 * 支付订单
	 * @return
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public AjaxJson payOrder(Member member, OrdersStore order) {
		AjaxJson aj = new AjaxJson();
		//查询购买商品
//		GoodsStore goodsStore = goodsStoreService.get(order.getGoodsStore().getId());
//		//积分
//		Double payIntegral = order.getGoodsPrice()*order.getGoodsNum();
//
//		//查询账户信息
//		String accountId = member.getAccount().getId();
//		Account account = accountService.get(accountId);
//		if (account==null){
//			aj.setMsg("账户信息错误");
//			aj.setSuccess(false);
//			return aj;
//		}
//		Double integral = account.getIntegral();
//
//		if (integral < payIntegral){
//			aj.setMsg("积分不足");
//			aj.setSuccess(false);
//			return aj;
//		}
//		//购买用户扣积分
//		account.setIntegral(integral-payIntegral);
//		accountService.save(account);
//		order.setOrdersStatus("2");
//		order.setIsExchange("0");
//		order.setJifen(payIntegral);
//		order.setPaytime(new Date());
//		save(order);
//
//		//增加该商品的购买人数
//		goodsStore.setBoughtPerson(goodsStore.getBoughtPerson()+order.getGoodsNum());
//		goodsStoreService.save(goodsStore);
//		//积分中心加积分
//		Member integralMember = memberService.get(order.getSpec());
//		Account integralAccount = accountService.get(integralMember.getAccount().getId());
//		integralAccount.setIntegral(integralAccount.getIntegral()+payIntegral);
//		accountService.save(integralAccount);
//
//		//商品兑换
//		IntegralDetail integralDetail = new IntegralDetail();
//		integralDetail.setMember(member);
//		integralDetail.setIntegralType("1");
//		integralDetail.setIntegral(payIntegral);
//		integralDetail.setOrderNo(order.getOrdersNo());
//		integralDetailService.save(integralDetail);
//		//会员兑换
//		IntegralDetail integralDetail1 = new IntegralDetail();
//		integralDetail1.setMember(integralMember);
//		integralDetail1.setIntegralType("2");
//		integralDetail1.setIntegral(payIntegral);
//		integralDetail1.setOrderNo(order.getOrdersNo());
//		integralDetailService.save(integralDetail1);
//
//		//处理服务中心积分兑换收益
//		centerRecordService.manageCenterIntegral(member,payIntegral);
//
//		aj.setMsg("支付成功");
//		aj.setSuccess(true);

		return aj;
	}

	/**
	 * 兑换
	 * @param member
	 * @param orderNo
	 * @return
	 */
	@Transactional(readOnly = false)
	public AjaxJson goToExchange(Member member, String orderNo) {
		AjaxJson aj = new AjaxJson();
		OrdersStore byNo = getByNo(orderNo);
		Account account = accountService.get(member.getAccount().getId());
		if (account != null){
			if (account.getIntegral()>=byNo.getJifen()){
				account.setIntegral(account.getIntegral()-byNo.getJifen());
				accountService.save(account);
				byNo.setIsExchange("1");
				save(byNo);
				//积分明细记录
				IntegralDetail integralDetail = new IntegralDetail();
				integralDetail.setIntegral(byNo.getJifen());
				integralDetail.setOrderNo(byNo.getOrdersNo());
				integralDetail.setIntegralType("3");
				integralDetail.setMember(member);
				integralDetailService.save(integralDetail);
				//平台积分
				List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
				if (rewardParametersList.size()>0){
					rewardParametersList.get(0).setTotalIntegral(rewardParametersList.get(0).getTotalIntegral() + byNo.getJifen());
					rewardParametersService.save(rewardParametersList.get(0));
				}
				//处理服务中心积分兑换收益
//				centerRecordService.manageCenterIntegral(member,byNo.getJifen());
			}else {
				aj.setSuccess(false);
				aj.setMsg("积分不足");
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("账户信息有误");
		}
		return aj;
	}

	/**
	 * 发货
	 * @param member
	 * @param orderNo
	 * @param expressName
	 *@param expressNo @return
	 */
	@Transactional(readOnly = false)
	public AjaxJson deliverGoods(Member member, String orderNo, String expressName, String expressNo) {
		AjaxJson aj = new AjaxJson();
		boolean flag = false;
		OrdersStore ordersStore = getByNo(orderNo);
		//查询持有库存是否充足
		CenterPosition centerPosition = new CenterPosition();
		centerPosition.setMember(member);
		centerPosition.setGoodsStore(ordersStore.getGoodsStore());
		List<CenterPosition> list = centerPositionService.findList(centerPosition);
		if (list.size() > 0){
			if (list.get(0).getStock() < 10){
				flag = true;
			}
//			if (list.get(0).getStock() <= 0){
//				aj.setMsg("库存不足");
//				aj.setSuccess(false);
//				aj.setErrorCode("0");
//				return aj;
//			}
//			//减库存
//			list.get(0).setStock(list.get(0).getStock() - ordersStore.getGoodsNum());
//			centerPositionService.save(list.get(0));
		}else {
			flag = true;
//			aj.setMsg("暂无库存，请补货");
//			aj.setErrorCode("0");
//			aj.setSuccess(false);
//			return aj;

		}
		//添加发货信息
		Express express = new Express();
		express.setOrderForm(expressNo);
		express.setMember(member);
		express.setTime(new Date());
		express.setGongsId(expressName);
		express.setOrdersstore(ordersStore);
		expressService.save(express);
		//更改订单状态
		ordersStore.setOrdersStatus("3");
		save(ordersStore);
		if (flag){
			aj.setMsg("发货成功。该商品库存不足，请及时补货！");
		}else {
			aj.setMsg("发货成功");
		}
		aj.setSuccess(true);
		return aj;
	}

	public List<OrdersStore> findLimitList(OrdersStore ordersStore) {
		List<OrdersStore> list = mapper.findLimitList(ordersStore);
		if (list.size()>0){
			for (OrdersStore store : list) {
				GoodsSpec goodsSpec = goodsSpecService.get(store.getSpec());
				if (goodsSpec != null){
					store.setSpecName(goodsSpec.getSpecName());
					store.setGoodsSpec(goodsSpec);
					store.setGoodsPrice(goodsSpec.getSpecPrice());
					store.setSendIntegral(goodsSpec.getSpecIntegral());
				}else {
					GoodsStore goodsStore = goodsStoreService.get(store.getGoodsStore().getId());
					if (goodsStore != null){
						store.setSendIntegral(goodsStore.getIntegral());
					}

				}

			}
		}
		return list;
	}

	/**
	 * 取消发货
	 * @param ordersStore
	 */
	@Transactional(readOnly = false)
	public void cancelExpressForOrder(OrdersStore ordersStore) {
		if ("2".equals(ordersStore.getOrdersStatus())){
			Double integral = ordersStore.getJifen() == null ? 0.0 : ordersStore.getJifen();
			Double money = ordersStore.getMoney() == null ? 0.0 : ordersStore.getMoney();
			//订单是余额支付的话，只返余额和积分中心库存，积分支付的返还积分，各收益及库存
			if (money > 0){
				//查询购买用户，返还余额
				Member member = memberService.get(ordersStore.getMember().getId());
				if (member != null){
					Account account = accountService.get(member.getAccount().getId());
					if (account != null){
						account.setYue(account.getYue() + ordersStore.getMoney());
						accountService.save(account);
					}
				}
				//查询对应积分中心返还库存
				Member mem = memberService.get(ordersStore.getSpec());
				if (mem != null){
					Double centerIntegral = 0.0;
					Double centerProfit = 0.0;
					CenterPosition centerPosition = new CenterPosition();
					centerPosition.setGoodsStore(ordersStore.getGoodsStore());
					centerPosition.setMember(mem);
					List<CenterPosition> centerPositions = centerPositionService.findList(centerPosition);
					if (centerPositions.size() > 0){
						centerPositions.get(0).setStock(centerPositions.get(0).getStock() + ordersStore.getGoodsNum());
						centerPositionService.save(centerPositions.get(0));
					}
				}
			}else if (integral > 0){
				//查询购买用户，返还积分
				Member member = memberService.get(ordersStore.getMember().getId());
				if (member != null){
					Account account = accountService.get(member.getAccount().getId());
					if (account != null){
						IntegralDetail integralDetail = new IntegralDetail();
						integralDetail.setMember(member);
						integralDetail.setOrderNo(ordersStore.getOrdersNo());
						List<IntegralDetail> integralDetailList = integralDetailService.findList(integralDetail);
						if (integralDetailList.size() > 0){
							Double buyIntegral = integralDetailList.stream().mapToDouble(IntegralDetail::getIntegral).sum();
							//逻辑删除
							integralDetailService.deleteAllByLogic(integralDetailList);
							account.setIntegral(account.getIntegral() + buyIntegral);
							accountService.save(account);
						}
					}
				}
				//查询对应积分中心返还库存
				//查询积分中心记录，减去对应积分，余额收益逻辑删除对应记录
				Member mem = memberService.get(ordersStore.getSpec());
				if (mem != null){
					Double centerIntegral = 0.0;
					Double centerProfit = 0.0;
					CenterPosition centerPosition = new CenterPosition();
					centerPosition.setGoodsStore(ordersStore.getGoodsStore());
					centerPosition.setMember(mem);
					List<CenterPosition> centerPositions = centerPositionService.findList(centerPosition);
					if (centerPositions.size() > 0){
						centerPositions.get(0).setStock(centerPositions.get(0).getStock() + ordersStore.getGoodsNum());
						centerPositionService.save(centerPositions.get(0));
					}
					IntegralDetail integralDetail = new IntegralDetail();
					integralDetail.setOrderNo(ordersStore.getOrdersNo());
					integralDetail.setMember(mem);
					List<IntegralDetail> integralDetailList = integralDetailService.findList(integralDetail);
					if (integralDetailList.size() > 0){
						//逻辑删除
						integralDetailService.deleteAllByLogic(integralDetailList);
						centerIntegral = integralDetailList.stream().mapToDouble(IntegralDetail::getIntegral).sum();
					}
					TeamRecord teamRecord = new TeamRecord();
					teamRecord.setMember(mem);
					teamRecord.setRemarks(ordersStore.getOrdersNo());
					List<TeamRecord> teamRecordList = teamRecordService.findList(teamRecord);
					if (teamRecordList.size() > 0){
						centerProfit = teamRecordList.stream().mapToDouble(TeamRecord::getMoney).sum();
						teamRecordService.deleteAllByLogic(teamRecordList);
					}
					Account account = accountService.get(mem.getAccount().getId());
					if (account != null){
						account.setIntegral(account.getIntegral() - centerIntegral);
						account.setYue(account.getYue() - centerProfit);
						account.setTotalProfit(account.getTotalProfit() - centerProfit);
						account.setTeamProfit(account.getTeamProfit() - centerProfit);
						accountService.save(account);
					}
				}
				//查询服务中心记录，减去对应收益，逻辑删除对应记录
				CenterRecord centerRecord = new CenterRecord();
				centerRecord.setOrderNo(ordersStore.getOrdersNo());
				centerRecord.setCatchType("1");
				List<CenterRecord> centerRecords = centerRecordService.findList(centerRecord);
				if (centerRecords.size() > 0){
					for (CenterRecord record : centerRecords) {
						Member member1 = memberService.get(record.getMember().getId());
						if (member1 != null){
							Account account = accountService.get(member1.getAccount().getId());
							if (account != null){
								account.setYue(account.getYue() - record.getMoney());
								account.setDynamicProfit(account.getDynamicProfit() - record.getMoney());
								account.setTotalProfit(account.getTotalProfit() - record.getMoney());
								accountService.save(account);
							}
						}
					}
					//逻辑删除
					centerRecordService.deleteAllByLogic(centerRecords);
				}
			}
			//更改订单状态
			ordersStore.setOrdersStatus("5");
			super.save(ordersStore);
		}
	}
}

